package org.vsb.chmireader.service;

import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.vsb.chmireader.log.LogUtils;
import org.vsb.chmireader.spring.configuration.NodeInfo;
import org.vsb.chmireader.utils.tasks.ReadURLTask;

@Service
public class WebReaderScheduler {
	
	static final Logger logger = LogUtils.getLogForMyClass();

	@Autowired
	@Qualifier("executorWithPoolSizeRange")
	private TaskExecutor executor;
	
	@Resource(name="nodeList")
	private ArrayList<NodeInfo> nodeListArray;

	@PostConstruct
	public void init() {
		logger.debug("scheduler is running every 6 hours(21600000 milis)");
		System.out.println("init");
	}

	@Scheduled(fixedRate = 21600000)//12 h = 43200000, 6h = 21600000
	public void readWeb() {
		for(NodeInfo ni:nodeListArray){
		executor.execute(new ReadURLTask(ni.getUrl(), ni.getName()));
}
}

}
